import { deleteShopSearch, deletesysConfig, getCategoryList, getShopSearch, getsysConfig, increaseConfig, increaseShopSearch, modifyConfig, modifyShopSearch} from '@/services';
import { categoryType, hotSearchtype, sysConfigtype } from '@/types';
import { Effect,Reducer } from 'umi';

export interface CategoryModelState {
  categoryList:categoryType[],
  hotsearchList:hotSearchtype[],
  hotsearchLength:number,
  sysconfigList:sysConfigtype[],
  sysconfigListlength:number,
}

export interface CategoryModelType {
  namespace: 'category';
  state: CategoryModelState;
  effects: {
    getCategoryList:Effect;
    getShopSearch:Effect;
    deleteShopSearch:Effect;
    increaseShopSearch:Effect;
    getsysConfig:Effect;
    deletesysConfig:Effect;
    increaseConfig:Effect;
    modifyShopSearch:Effect;
    modifyConfig:Effect;
  };
  reducers: {
    save: Reducer<CategoryModelState>;
  };
}

const categoryModel: CategoryModelType = {
  // 数据模型，用来隔离数据防止相互影响
  namespace: 'category',

  // 数据模型状态，相当于vuex的state
  state: {
    categoryList:[],
    hotsearchLength:0,
    hotsearchList:[],
    sysconfigList:[],
    sysconfigListlength:0,
  },

  // 异步操作，相当于vuex的action
  effects: {
    *getCategoryList({ payload }, { call, put }) {
      let result = yield call(getCategoryList, payload); 
       if (result.data) {
        yield put({
          type: 'save',
          payload: {
            categoryList: result.data
          }
        })
      }
    },
    *getShopSearch({ payload }, { call, put }) {
      let result = yield call(getShopSearch, payload);
       if (result.records) {
        yield put({
          type: 'save',
          payload: {
            hotsearchList: result.records,
            hotsearchLength:result.records.length
          }
        })
      }
    },
    *deleteShopSearch({ payload }, { call, put }) {      
      let result = yield call(deleteShopSearch, payload);
    },
    *increaseShopSearch({ payload }, { call, put }) {      
      let result = yield call(increaseShopSearch, payload);
    },
    *modifyShopSearch({ payload }, { call, put }) {    
      let result = yield call(modifyShopSearch, payload); 
    },
    *getsysConfig({ payload }, { call, put }) {
      let result = yield call(getsysConfig, payload);
      console.log('159',result.records);
       if (result.records) {
        yield put({
          type: 'save',
          payload: {
            sysconfigList: result.records,
            sysconfigListlength:result.records.length
          }
        })
      }
    },
    *deletesysConfig({ payload }, { call, put }) {      
      let result = yield call(deletesysConfig, payload);   
    },
    *increaseConfig({ payload }, { call, put }) {      
      let result = yield call(increaseConfig, payload);      
    },
    *modifyConfig({ payload }, { call, put }) {    
      let result = yield call(modifyConfig, payload);
      console.log('11111',result);
    },
  },

  // 同步操作，相当于vuex的mutation
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },

  }
};

export default categoryModel;

